﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Technullogy
{
    public static class EmptyDirectory
    {
        private static List<String> emptyDirectories;
        public static List<String> GetEmptyDirectories(String rootPath)
        {
            if (emptyDirectories == null)
                emptyDirectories = new List<String>();

            foreach (var dir in Directory.GetDirectories(rootPath))
            {
                if (Directory.GetDirectories(dir).Count() == 0)
                {
                    var files = Directory.GetFiles(dir);
                    if (files.Count() > 0)
                    {
                        foreach (var file in files)
                        {
                            if (TechnullogyConfiguration.FileTypesToDelete.Contains(Path.GetExtension(file).ToLower()))
                            {
                                TechnullogyLogging.LogFileDeleted(file);
                                File.Delete(file);
                            }
                            else
                                TechnullogyLogging.LogFileSkipped(file);
                        }
                    }

                    if (Directory.GetFiles(dir).Count() == 0)
                        emptyDirectories.Add(dir);
                }

                DirectorySync.LoadDirectories(dir);
            }

            return emptyDirectories;
        }

        public static void DeleteEmptyDirectories(List<String> directories)
        {
            TechnullogyLogging.LogDirectoryDeleteStarted(DateTime.Now);

            foreach (var item in directories)
            {
                if (Directory.Exists(item))
                {
                    TechnullogyLogging.LogDirectoryDelete(item);
                    Directory.Delete(item);
                }
            }
        }
    }
}
